<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!--分账按钮-->
			<!-- <el-button type="primary" @click="distribute">分账</el-button> -->
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "order-shop",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import citySelect from '../components/city-select.vue';
import merchantSelect from '../components/merchant-select.vue';

const { service } = useCool();
const { t } = useI18n();

// cl-upsert
const Upsert = useUpsert({
	items: [
		// 城市选择
		// {
		// 	label: t("城市"),
		// 	prop: "cityId",
		// 	component: {
		// 		vm: citySelect,
		// 		props: {
		// 			modelValue: '' // 初始值
		// 		}
		// 	},
		// 	span: 12,
		// },
		{
			label: t("城市"),
			prop: "cityId",
			component: {
				name: 'el-select',
				options: []
			},
			span: 12,
		},
		{
			label: t("名称"),
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("地址"),
			prop: "address",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("接诊时间"),
			prop: "acceptTime",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		{
			label: t("电话"),
			prop: "phone",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		// {
		// 	label: t("头图"),
		// 	prop: "headerImage",
		// 	component: { name: "cl-upload" },
		// },
		// {
		// 	label: t("详情图"),
		// 	prop: "detailImages",
		// 	component: { name: "cl-upload", props: { multiple: true } },
		// },
		{
			label: t("负责人"),
			prop: "principal",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("经度"),
			prop: "longitude",
			hook: "number",
			component: { name: "el-input-number", props: { min: 0 } },
			span: 12,
		},
		{
			label: t("纬度"),
			prop: "latitude",
			hook: "number",
			component: { name: "el-input-number", props: { min: 0 } },
			span: 12,
		},
		// 收货人
		{
			label: t("收货人"),
			prop: "contactName",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		// 收货电话
		{
			label: t("收货电话"),
			prop: "contactPhone",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		// 收货地址
		{
			label: t("收货地址"),
			prop: "receiveAddress",
			component: { name: "el-input", props: { clearable: true } },
			span: 24,
		},
		// // 支付商户ID
		// {
		// 	label: t("支付商户"),
		// 	prop: "payMerchantId",
		// 	component: {
		// 		vm: merchantSelect,
		// 		props: {
		// 			modelValue: '' // 初始值
		// 		}
		// 	},
		// 	span: 12,
		// },
		// // 代理商商户ID
		// {
		// 	label: t("代理商商户"),
		// 	prop: "agentMerchantId",
		// 	component: {
		// 		vm: merchantSelect,
		// 		props: {
		// 			modelValue: '' // 初始值
		// 		}
		// 	},
		// 	span: 12,
		// },
		// // 售卖商品ID
		// {
		// 	label: t("商品ID"),
		// 	prop: "goodsId",
		// 	component: { name: "el-input", props: { clearable: true } },
		// 	span: 12,
		// },
		// {
		// 	label: t('代理商'),
		// 	prop: 'agentId',
		// 	component: {
		// 		name: 'el-select',
		// 		options: []
		// 	},
		// 	span: 12
		// },
	],
	async onOpened() {
		// 设置城市列表
		service.order.city.getCityList().then(res => {
			console.log(res)
			Upsert.value?.setOptions(
				'cityId',
				res.map(e => {
					return {
						label: e.label || '',
						value: e.value,
					}
				})
			)
		});
		// 设置代理商列表
		// service.base.sys.user.getAgentList().then(res => {
		// 	Upsert.value?.setOptions(
		// 		'agentId',
		// 		res.map(e => {
		// 			return {
		// 				label: e.label || '',
		// 				value: e.value,
		// 			}
		// 		})
		// 	)
		// });
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ label: t("ID"), prop: "id", minWidth: 80 },
		{ label: t("城市"), prop: "cityName", minWidth: 140 },
		{ label: t("名称"), prop: "name", minWidth: 140 },
		{ label: t("地址"), prop: "address", minWidth: 140 },
		{ label: t("接诊时间"), prop: "acceptTime", minWidth: 140 },
		{ label: t("电话"), prop: "phone", minWidth: 140 },
		// {
		// 	label: t("头图"),
		// 	prop: "headerImage",
		// 	minWidth: 100,
		// 	component: { name: "cl-image", props: { size: 60 } },
		// },
		// {
		// 	label: t("详情图"),
		// 	prop: "detailImages",
		// 	minWidth: 100,
		// 	component: { name: "cl-image", props: { size: 60 } },
		// },
		{ label: t("负责人"), prop: "principal", minWidth: 140 },
		{
			label: t("经度"),
			prop: "longitude",
			minWidth: 140,
			sortable: "custom",
		},
		{
			label: t("纬度"),
			prop: "latitude",
			minWidth: 140,
			sortable: "custom",
		},
		{ label: t("收货人"), prop: "contactName", minWidth: 140 },
		{ label: t("收货电话"), prop: "contactPhone", minWidth: 140 },
		{ label: t("收货地址"), prop: "receiveAddress", minWidth: 140 },

		// {
		// 	label: t("创建时间"),
		// 	prop: "createTime",
		// 	minWidth: 170,
		// 	sortable: "desc",
		// 	component: { name: "cl-date-text" },
		// },
		// {
		// 	label: t("更新时间"),
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.order.shop,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
async function distribute() {
	const result = await service.order.order.share({
		orderId: 83
	});
}
</script>
